﻿
using System;
using SqlSugar;
using System.Threading.Tasks;
using System.Linq.Expressions;

using Oliving.Edge.Model;
using Oliving.Edge.Model.Dto;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IRepository;
using Oliving.Edge.IRepository.UnitOfWork;
using Oliving.Edge.Repository.Base;
using Oliving.Edge.Model.Dto.XLT;
using Oliving.Edge.IRepository.XLT;
using Oliving.Edge.Model.Models.XLT;
using Oliving.Edge.Model.Dto.Xcx;

namespace Oliving.Edge.Repository.XLT
{
    /// <summary>
    /// CD_CleaningPointRepository
    /// </summary>
    public class CD_CleaningPointRepository : BaseRepository<CD_CleaningPoint>, ICD_CleaningPointRepository
    {
        public CD_CleaningPointRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }






        /// <summary>
        /// 自定义分页查询
        /// </summary>
        /// <param name="whereExpression">查询条件</param>
        /// <param name="pageIndex" > 页码（下标0）</param>
        /// <param name="pageSize" > 页大小</param>
        /// <param name="sortField" > 排序字段</param>
        /// <param name="sortOrder" > 排序顺序（asc/desc）</param>
        /// <returns></returns>
        public async Task<PageModel<CD_CleaningPointPageDto>> QueryPageList(
                Expression<Func<CD_CleaningPoint, bool>> whereExpression,
                int pageIndex = 1, int pageSize = 20,
                string sortField = null, string sortOrder = null
                )
        {


            #region 排序

            string strOrderByFileds = null;

            if (!string.IsNullOrEmpty(sortField))
            {
                strOrderByFileds = sortField;
                if (sortOrder != null && sortOrder.ToLower().Contains("desc"))
                {
                    strOrderByFileds = strOrderByFileds + " desc";
                }
                else
                {
                    strOrderByFileds = strOrderByFileds + " asc";
                }
            }

            #endregion

            if (1 == 1)
            {
                #region 单表

                RefAsync<int> totalCount = 0;
                var list = await Db.Queryable<CD_CleaningPoint>()
                         .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                         .WhereIF(whereExpression != null, whereExpression)
                         .Select<CD_CleaningPointPageDto>()
                         .ToPageListAsync(pageIndex, pageSize, totalCount);

                int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt();
                return new PageModel<CD_CleaningPointPageDto>()
                {
                    dataCount = totalCount,
                    pageCount = pageCount,
                    page = pageIndex,
                    PageSize = pageSize,
                    data = list
                };

                #endregion
            }

            {
                //var list = Db.Queryable<DTSN_Canning, DTSN_TongDao>
                //   ((c, td) => new object[] { JoinType.Left, c.Canning_TongDaoID == td.TD_ID })
                // //.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                // .WhereIF(whereExpression != null, whereExpression)
                // .GroupBy((c, td) => td.TD_Type)
                // .GroupBy((c, td) => Convert.ToDateTime(c.Canning_BeginTime).ToShortDateString())
                //   .Select((c, td) => new
                //   {
                //       date = Convert.ToDateTime(c.Canning_BeginTime).ToShortDateString(),
                //       type = td.TD_Type,
                //       sum = SqlFunc.AggregateSum(c.Canning_NetWeight)
                //   }).ToList();
            }


        }
        #region 根据扫码的保洁点ID 查询保洁点信息  白克平 2020-9-15
        /// <summary>
        ///  根据扫码的保洁点ID 查询保洁点信息
        ///  <author>白克平</author>
        ///  <datetime>2020-9-15</datetime>
        /// </summary>
        /// <param name="id">保洁点id</param>
        /// <returns></returns>
        public async Task<xCleaningPoint> QueryById(string id)
        {
            var data = await Db.Queryable<CD_CleaningPoint, CD_Area>((clean, area) => new object[] {
            JoinType.Inner,clean.AreaId==area.AreaId}).Where((clean, area) => clean.CleaningPointId == id && clean.Enabled == 1 && area.Enabled == 1)
            .Select((clean, area) => new xCleaningPoint
            {
                CleaningPointId = clean.CleaningPointId,
                AreaAddress = area.AreaAddress,
                Category = clean.Category,
                AreaTel = area.AreaTel,
                CleaningPointTime = clean.CleaningPointTime,
                ProjectId = clean.ProjectId,
                GridId = clean.GridId,
                AreaId = clean.AreaId
            })
            .FirstAsync();
            return data;
        }
        #endregion

        #region 非保洁人员扫码根据扫码的保洁点ID 查询保洁点信息  白克平 2020-9-15
        /// <summary>
        ///  非保洁人员扫码根据扫码的保洁点ID 查询保洁点信息
        ///  <author>白克平</author>
        ///  <datetime>2020-9-15</datetime>
        /// </summary>
        /// <param name="id">保洁点id</param>
        /// <returns></returns>
        public async Task<FBjInfo> FQueryById(string id)
        {
            var data = await Db.Queryable<CD_CleaningPoint, CD_Area, CD_CleaningRecord>((clean, area, creord) => new object[] {
            JoinType.Inner,clean.AreaId==area.AreaId,
            JoinType.Left,clean.CleaningPointId==creord.CleaningPointId
            }).Where((clean, area, creord) => clean.CleaningPointId == id && clean.Enabled == 1 && area.Enabled == 1)
            .Select((clean, area, creord) => new FBjInfo
            {
                CleaningPointId = clean.CleaningPointId,
                AreaAddress = area.AreaAddress,
                Category = clean.Category,
                AreaTel = area.AreaTel,
                CleaningPointTime = clean.CleaningPointTime,
                ProjectId = clean.ProjectId,
                GridId = clean.GridId,
                AreaId = clean.AreaId,
                CreateUserName = clean.CreateUserName,
                Result = creord.RESULT,
                CreateDate = creord.CreateDate
            })
            .FirstAsync();
            return data;
        }
        #endregion

        #region 保洁记录  白克平 2020-9-15
        /// <summary>
        ///  保洁记录 
        ///  <author>白克平</author>
        ///  <datetime>2020-9-15</datetime>
        /// </summary>
        /// <param name="whereExpression">条件</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">条数</param>
        /// <returns></returns>
        public async Task<PageModel<FBjInfo>> GetPageList(Expression<Func<CD_CleaningRecord, bool>> whereExpression,
                int pageIndex = 1, int pageSize = 20)
        {
            RefAsync<int> totalCount = 0;
            var data = await Db.Queryable<CD_CleaningRecord, CD_CleaningPoint, CD_Area>((creord, clean, area) => new object[] {
            JoinType.Inner,clean.AreaId==area.AreaId,
            JoinType.Left,clean.CleaningPointId==creord.CleaningPointId
            }).WhereIF(whereExpression != null, whereExpression)
            .Select((creord, clean, area) => new FBjInfo
            {
                CleaningPointId = clean.CleaningPointId,
                AreaAddress = area.AreaAddress,
                Category = clean.Category,
                AreaTel = area.AreaTel,
                CleaningPointTime = clean.CleaningPointTime,
                ProjectId = clean.ProjectId,
                GridId = clean.GridId,
                AreaId = clean.AreaId,
                CreateUserName = clean.CreateUserName,
                Result = creord.RESULT,
                CreateDate = creord.CreateDate
            })
             .ToPageListAsync(pageIndex, pageSize, totalCount);
            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<FBjInfo>()
            {
                dataCount = totalCount,
                pageCount = pageCount,
                page = pageIndex,
                PageSize = pageSize,
                data = data
            };
        }
        #endregion
    }
}

